package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏成博
 * @date 2023-02-09 16:09
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐
        setmealMapper.saveSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为菜品添加套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        //新增套餐菜品表
        setmealDishMapper.saveSetMealDish(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐批量删除
     * @param ids
     */
    @Transactional
    public void deleteSetmeal(String ids) {
        //根据，切割字符串
        String[] split = ids.split(",");
            List<Integer> list = new ArrayList<>();
            Setmeal setmeal = null;
            //将其全部变为int类型加入list集合
            for (int i = 0; i < split.length; i++) {
                try {
                    int id = Integer.parseInt(split[i]);
                    setmeal = setmealMapper.findById(id);
                    if (setmeal.getStatus() == 1) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                    }
                    list.add(id);
                } catch (NumberFormatException e) {
                    throw new DeletionNotAllowedException(MessageConstant.ERROR_SETMEAL);
                }
            }
        setmealMapper.deleteByIds(list);
    }

    /**
     * 更新套餐信息
     * @param status
     * @param id
     */
    public void updateStatus(Integer status,Long id) {
        Setmeal setmeal=new Setmeal().builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    public SetmealVO findById(Integer id) {
        //创建setmealVO对象
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal=setmealMapper.findById(id);
        //将查出来的Setmeal对象属性复制给setmealVO
        BeanUtils.copyProperties(setmeal,setmealVO);
        //查出其关联了几条dish
        List<SetmealDish> dishes = setmealDishMapper.findBySetmealId(id);
        //封装进setmealVO
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    /**
     * 更新套餐信息
     * @param setmealDTO
     */
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除setmeal_dish表中的信息
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为其设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        //添加setmeal_dish表信息
        setmealDishMapper.saveSetMealDish(setmealDishes);
    }

    /**
     * 根据分类id查询套餐，返回集合
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> findSetmeal(Setmeal setmeal) {
        List<Setmeal> setmealList=setmealMapper.findSetmeal(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查出与其关联的菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> findSetmealDishes(Integer setmealId) {
       List<DishItemVO> dishItemVOList= setmealMapper.findSetmealDishes(setmealId);
        return dishItemVOList;
    }

    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal=setmealMapper.findById(id.intValue());
        return setmeal;
    }
}
